package com.example.demo.sqlInjector;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.example.demo.annotation.Join;
import com.example.demo.annotation.JoinField;
import com.example.demo.util.GlobalUtil;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;

public class MySelectList extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        List<String> whereSql = new ArrayList<>();
        List<String> selectSql = new ArrayList<>();
        List<String> joinSql = new ArrayList<>();
//        getAllSql(tableInfo, whereSql,selectSql, joinSql,false);
        MySqlUtil.getAllSqlFromClass(modelClass, whereSql,selectSql, joinSql,false);
        String sql = "<script>select %s from %s %s  %s</script>";
        sql = String.format(sql,selectSql.stream().collect(joining(",")),tableInfo.getTableName(),joinSql.stream().collect(joining(" ")),SqlScriptUtils.convertWhere(whereSql.stream().collect(Collectors.joining(" "))));
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, "mySelectList", sqlSource, tableInfo);
    }

//
//    //首字母小写如UserName 转 userName
//    private String unCapSimpleName(Class clazz) {
////        return org.springframework.util.StringUtils.uncapitalize(clazz.getSimpleName());
//        return StringUtils.firstToLowerCase(clazz.getSimpleName());
//    }
//
//    void getAllSql(TableInfo tableInfo, List<String> whereSql, List<String> selectSql,List<String> joinSql, boolean isJoin) {
//        selectSql.add(getKeySelectSql(tableInfo,isJoin));
//        //获取本体的
//        for (TableFieldInfo tableField : tableInfo.getFieldList()) {
//            JoinField joinField = tableField.getField().getAnnotation(JoinField.class);
//            if (joinField != null) {
//                getAllSql(TableInfoHelper.initTableInfo(this.builderAssistant,tableField.getPropertyType()), whereSql, selectSql,joinSql, true);
//                //生成JoinSql
//                joinSql.add(getJoinSql(tableInfo,tableField,joinField));
//            } else { //普通参数
//                whereSql.add(getWhereSql(tableInfo, tableField, isJoin));
//                selectSql.add(getSelectSql(tableInfo, tableField,isJoin));
//            }
//        }
//    }
//
//
//
//
//
//    String getJoinSql(TableInfo tableInfo,TableFieldInfo tableField,JoinField joinField){
//        //生成JoinSql
//        String fk1 = joinField.fk();
//        if(joinField.fk()==null||joinField.fk().equals("")){
//            fk1 = StringUtils.camelToUnderline(tableField.getColumn())+"_id";
//        }
//        fk1 = unCapSimpleName(tableInfo.getEntityType())+DOT+fk1;
//        String fk2 = tableField.getEl()+DOT+"id";
//        return String.format("left join %s as %s on %s = %s ",tableField.getColumn(),tableField.getEl(),fk1,fk2);
//    }
//
//
//
//    String getWhereSql(TableInfo tableInfo, TableFieldInfo fieldInfo, Boolean isJoin) {
//        String whereColumn = AND + " " + unCapSimpleName(tableInfo.getEntityType()) + DOT + fieldInfo.getColumn();
//        String wherePropertyPrefix = WRAPPER_ENTITY;
//        if (isJoin) {
//            wherePropertyPrefix = WRAPPER_ENTITY_DOT + unCapSimpleName(tableInfo.getEntityType());
//        }
//        String whereProperty = wherePropertyPrefix + DOT + fieldInfo.getEl();
//        String sql;
//        if(fieldInfo.getField().getType() == String.class){ //字符
//            sql = whereColumn + " like " + String.format("concat('%%',#{%s},'%%')", whereProperty);
//        } else {
//           sql = whereColumn + " = " + String.format("#{%s}", whereProperty);
//        }
//        sql = SqlScriptUtils.convertIf(sql, String.format("%s !=null and %s !=null", wherePropertyPrefix, whereProperty), true);
//        //如果有时间
//        if (fieldInfo.getField().getType() == Date.class){//时间 >= Start <=End
//           String sq = whereColumn + "&gt;=" + String.format("#{%sStart}", whereProperty) + NEWLINE + whereColumn + "&lt;=" + String.format("#{%sEnd}", whereProperty);
//           sq = SqlScriptUtils.convertIf(sq, String.format("%s !=null and %sStart !=null  and %sEnd !=null", wherePropertyPrefix, whereProperty,whereProperty), true);
//           sql+=sq;
//        }
//
//        return sql;
//    }
//
//
//
//    String getSelectSql(TableInfo tableInfo, TableFieldInfo fieldInfo,Boolean isJoin) {
//        String sqlColumn = unCapSimpleName(tableInfo.getEntityType()) + DOT + fieldInfo.getColumn();
//        String sqlProperty = fieldInfo.getEl();
//        if(isJoin){
//            sqlProperty = unCapSimpleName(tableInfo.getEntityType()) + DOT + fieldInfo.getEl();
//        }
//
//        return String.format("%s as '%s'", sqlColumn, sqlProperty);
//    }
//
//
//
//    String getKeySelectSql(TableInfo tableInfo,Boolean isJoin) {
//        String sqlColumn = unCapSimpleName(tableInfo.getEntityType()) + DOT + tableInfo.getKeyColumn();
//        String sqlProperty = tableInfo.getKeyProperty();
//        if(isJoin){
//            sqlProperty = unCapSimpleName(tableInfo.getEntityType()) + DOT + sqlProperty;
//        }
//        return String.format("%s as '%s'", sqlColumn, sqlProperty);
//    }

}
